package com.isunland.managesystem.adapter;

import android.support.annotation.NonNull;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.PopupMenu;
import android.widget.TextView;
import butterknife.BindView;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.BaseButterKnifeAdapter;
import com.isunland.managesystem.base.BaseVolleyActivity;
import com.isunland.managesystem.entity.companymemorabiliazi;
import com.isunland.managesystem.utils.FileUtil;
import java.util.ArrayList;

/**
 * 对象功能:
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: wangyanan
 * 创建时间: 2017/5/3 13:53
 */
public class FileAdapter extends BaseButterKnifeAdapter<companymemorabiliazi> {
  private boolean mIfDismissMore;//是否隐藏更多按钮.
  private Callback mCallback;
  private int mIconRes;

  public FileAdapter(BaseVolleyActivity context, ArrayList<companymemorabiliazi> objects,
      Callback callback) {
    super(context, objects);
    this.mCallback = callback;
  }

  @Override protected int setLayoutId() {
    return R.layout.adapter_file;
  }

  @NonNull @Override protected BaseViewHolder initHolder(View convertView) {
    return new ViewHolder(convertView);
  }

  @Override
  protected void initView(companymemorabiliazi item, BaseViewHolder baseHolder, int position) {
    ViewHolder holder = (ViewHolder) baseHolder;
    holder.tvFileName.setText(FileUtil.getFileNameByFilePath(item.getSdefpropValue()));
    holder.tvDate.setText(item.getRegDate());
    holder.ivMore.setTag(item);
  }

  private void showPopMenu(final View view) {
    if (!(view.getTag() instanceof companymemorabiliazi)) return;
    final companymemorabiliazi itemObj = (companymemorabiliazi) view.getTag();
    PopupMenu popupMenu = new PopupMenu(context, view);
    popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
      @Override public boolean onMenuItemClick(MenuItem item) {
        switch (item.getItemId()) {
          case R.id.menu_item_delete:
            mCallback.delete(itemObj);
            return true;
          case R.id.menu_item_check:
            mCallback.check(itemObj);
            return true;
        }
        return false;
      }
    });

    popupMenu.inflate(R.menu.menu_check_delete);
    popupMenu.show();
  }

  public void setIfDismissMore(boolean mIfDismissMore) {
    this.mIfDismissMore = mIfDismissMore;
  }

  public void setmIconRes(int mIconRes) {
    this.mIconRes = mIconRes;
  }

  public interface Callback {
    void delete(companymemorabiliazi item);

    void check(companymemorabiliazi item);
  }

  protected class ViewHolder extends BaseViewHolder {
    @SuppressWarnings("unused") @BindView(R.id.iv_file) ImageView ivFile;
    @BindView(R.id.tv_fileName) TextView tvFileName;
    @BindView(R.id.tv_date) TextView tvDate;
    @BindView(R.id.iv_more) ImageView ivMore;

    ViewHolder(View view) {
      super(view);
      if (mIfDismissMore) ivMore.setVisibility(View.INVISIBLE);
      if (mIconRes != 0) ivFile.setImageResource(mIconRes);
      ivMore.setOnClickListener(new View.OnClickListener() {
        @Override public void onClick(View v) {
          showPopMenu(v);
        }
      });
    }
  }
}
